## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"

Maps, time-series, crashes

Here is the Leaflet that I created to display the mortage crisis in the United States.

As you can see, in most states the mortgage crisis had a strong effect on the number of permits given. However, it seems that it was already slowing down even before the actual crisis, so it might indicate that there were some signs of the crisis even before the known crisis years.

m <- leaflet() %>%
  setView(-96, 37.8, 4) %>%
  addTiles()

bins <- c(100, 500, 1000, 2000, 5000, 10000, 20000, 100000, 200000, Inf)

names(sp.point.df) %>%
  purrr::walk( function(df) {
    pal <- colorBin("YlOrRd", domain = sp.point.df[[df]]$value, bins = bins)
    
    m <<- m %>%
      addPolygons(data = sp.point.df[[df]],
                  group = paste(df, "States", sep = " - "),
                  fillColor = ~pal(value),
                  weight = 2,
                  opacity = 1,
                  color = "White",
                  dashArray = "3",
                  fillOpacity = 0.7,
                  popup = sp.point.df[[df]]$name
                  ) 
  })

bins <- c(0, 10, 50, 100, 500, 1000, 5000, 10000, 50000, Inf)

names(counties.df) %>%
  purrr::walk( function(df) {
    pal <- colorBin("YlOrRd", domain = counties.df[[df]]$value, bins = bins)

    m <<- m %>%
      addPolygons(data = counties.df[[df]],
                  # label = ~as.character(value),
                  group = paste(df, "Counties", sep = " - "),
                  fillColor = ~pal(value),
                  weight = 0.7,
                  opacity = 0.7,
                  color = "white",
                  dashArray = "3",
                  fillOpacity = 0.7,
                  popup = paste(counties.df[[df]]$state_name, paste(counties.df[[df]]$name, "County", sep = " "), sep = " - ")
                  )
  })

m %>% 
  addLayersControl(baseGroups = c(paste(names(sp.point.df), "Counties", sep = " - "), paste(names(sp.point.df), "States", sep = " - ")),
                   options = layersControlOptions(collapsed = FALSE))